import 'babel-polyfill'
import React from 'react'
import ReactDOM, { render } from 'react-dom'
import { syncHistoryWithStore } from 'react-router-redux'
import configureStore from './store/configureStore'
import DevTools from './containers/DevTools'
import { Router, browserHistory } from 'react-router'
import { ReduxAsyncConnect } from 'redux-async-connect'
import { Provider } from 'react-redux'
import getDynamicRoutes from './routes.dynamic'
import App from './containers/App/App'

const store = configureStore()
const rootRoute = {
  path: '/',
  component: App,
  childRoutes: getDynamicRoutes()
}

const dest = document.getElementById('root');
const history = syncHistoryWithStore(browserHistory, store)


const component = (
  <Router 
    render={(props) => 
      <ReduxAsyncConnect {...props} filter={item => !item.deferred} />
    }     
    history={history}
    routes={rootRoute}
  ></Router>
);


ReactDOM.render(
  <Provider store={store} key="provider">
    {component}
  </Provider>,
  dest
);


if (__DEV__ && !window.devToolsExtension) {
  ReactDOM.render(
    <Provider store={store} key="provider">
      <div>
        {component}
        <DevTools />
      </div>
    </Provider>,
    dest
  );
}
